/** @file  fim_common.c
 *  @brief fim
 *
 */

/*
//|----------------------------------------------------------------------------
//|                 SKY NETWORKS CONFIDENTIAL - COPYRIGHT 2016
//|----------------------------------------------------------------------------
//| The information contained herein is the property of Sky Networks and is
//| strictly confidential. Except as expressly authorized in writing by Sky
//| Networks, the holder shall keep all information contained herein confiden-
//| tial, shall disclose it only to its employees with a need to know, and
//| shall protect it, in whole or in part, from disclosure and dissemination
//| to third parties with the same degree of care it uses to protect its own
//| confidential information of like nature, but with no less than reasonable
//| care. Except expressly authorized in writing by Sky Networks, the holder
//| is granted no rights to use the information contained herein.
//|----------------------------------------------------------------------------
*/

/*
//|----------------------------------------------------------------------------
//| Flexelint
//|----------------------------------------------------------------------------
*/
/*lint -save */
/*lint +libclass(all) */
/* supress the flint warning 534 about return value */
/*lint -esym(534,sleep) */
/*
//|----------------------------------------------------------------------------
//| Includes
//|----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <pthread.h>
#include <sys/queue.h>
#include <signal.h>
#include <stdbool.h>
#include <fcntl.h>
#include "fim.h"

/*
//|--------------------------------------------------------------------------
//| Method     : fim_create_socket
//|--------------------------------------------------------------------------
*/
int fim_create_socket(const char * sock_name)
{
    struct sockaddr_un addr;
    socklen_t length;
    int rc;

    int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if (fd < 0)
    {
        printf("socket %s\n", strerror(errno));
        return 0;
    }
    if(sock_name == NULL)
    {
      printf("the sock_name is null \n");
      return 0;
    }

    (void)unlink(sock_name);

    memset ( (char *)&addr, 0,sizeof(addr) );
    addr.sun_family = AF_UNIX;
    strcpy(addr.sun_path, sock_name);

    length = sizeof(addr.sun_family) + (socklen_t)strlen(addr.sun_path);

    rc = bind(fd, (struct sockaddr*) ((void *)&addr), length);
    if (rc < 0)
    {
        printf("bind %s",strerror(errno));
        (void)close(fd);
        return 0;
    }
    return fd;
}

/*
//|--------------------------------------------------------------------------
//| Method     : fim_send_to
//|--------------------------------------------------------------------------
*/
int fim_send_to
(
  int          fd,
  const char * sock_name,
  const void * buffer,
  int          length,
  int          retry
)
{
  struct sockaddr_un addr;
  socklen_t          addr_length;
  int                rc;

  if(sock_name == NULL)
  {
    printf("the sock_name is null \n");
    return -1;
  }
  memset ((char *)&addr, 0, sizeof(addr) );
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, sock_name);

  addr_length = sizeof(addr.sun_family) + (socklen_t)strlen(addr.sun_path);

  while (((rc = sendto(fd, buffer, length, 0,
                       (struct sockaddr *)((void *)&addr), addr_length)) < 0) &&
         retry )
  {
    sleep (1);
  }

  if (rc < 0)
  {
    printf("sendto %s failed: %s",sock_name,strerror(errno));
    return -1;
  }

  return 0;
}
